package polarizedMF;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Step8b_Ordinal_MeasureF1Margin {

	/**
	 * @param args
	 */
	double posPrecision;
	double negPrecision;
	double posRecall;
	double negRecall;
	double posF1;
	double negF1;

	public Step8b_Ordinal_MeasureF1Margin(String svm_dataset, String fold,
			String epsilon, double margin) throws IOException {
		System.out.println("epsilon " + epsilon);
		System.out.println("margin " + margin);
		BufferedReader output = new BufferedReader(new FileReader(svm_dataset
				+ "fold-" + fold + "\\output0" + epsilon + ""));
		BufferedReader output2 = new BufferedReader(new FileReader(svm_dataset
				+ "fold-" + fold + "\\output0" + epsilon + "_"));
		
//		BufferedReader output = new BufferedReader(new FileReader(svm_dataset
//				+ "fold-" + fold + "\\outtrain0" + epsilon + ""));
//		BufferedReader output2 = new BufferedReader(new FileReader(svm_dataset
//				+ "fold-" + fold + "\\outtrain0" + epsilon + "_"));
		
		BufferedReader grdtruth = new BufferedReader(new FileReader(svm_dataset
				+ "fold-" + fold + "\\groundtruth0" + epsilon + ""));

		String line;
		double minThres = margin;
		int tp = 0;
		int fp = 0;
		int zp = 0;
		int np = 0;

		int nn = 0;
		int tn = 0;
		int fn = 0;
		double minThres2 = minThres;

		System.out.println("Min threshold: " + minThres);

		while ((line = output.readLine()) != null) {
			double confidence = Double.parseDouble(line);

			String lineGT = grdtruth.readLine();
			double truth = Double.parseDouble(lineGT);

			if (truth > 0) {
				np++;
			} else if (truth < 0) {
				nn++;
			}

			if (confidence > 0) {

				if (confidence > minThres) {
					if (truth > 0) {
						tp++;
					} else if (truth < 0) {
						fp++;
					} else {
						zp++;
					}
				}
			}

			else {
				String line2 = output2.readLine();
				double confidence2 = Double.parseDouble(line2);

				if (confidence2 > 0) {

					if (confidence2 > minThres2) {
						if (truth < 0) {
							tn++;

						} else {
							fn++;
						}
					}
				}
			}
		}
		output.close();
		output2.close();
		grdtruth.close();

		System.out.println("True pos: " + tp);
		System.out.println("False pos: " + fp);
		System.out.println("Z pos: " + zp);

		double posPrecision = 100 * tp / (double) (tp + fp + zp);
		double posRecall = 100 * tp / (double) np;
		double posF1 = 2 * posPrecision * posRecall
				/ (posPrecision + posRecall);

		this.posPrecision = posPrecision;
		this.posRecall = posRecall;
		this.posF1 = posF1;

		System.out.printf("Precision/Recall/F1: %.1f / %.1f / %.1f\n",
				posPrecision, posRecall, posF1);
		System.out
				.println("Number of true retrieved postive/ retrieved positive/ true positive: "
						+ tp + "/" + (tp + fp + zp) + "/" + np);

		double negPrecision = 100 * tn / (double) (tn + fn);
		double negRecall = 100 * tn / (double) nn;
		double negF1 = 2 * negPrecision * negRecall
				/ (negPrecision + negRecall);

		this.negPrecision = negPrecision;
		this.negRecall = negRecall;
		this.negF1 = negF1;

		System.out.printf("Precision/Recall/F1: %.1f / %.1f / %.1f\n",
				negPrecision, negRecall, negF1);
		System.out
				.println("Number of true retrieved negative/ retrieved negative/ true negative: "
						+ tn + "/" + (tn + fn) + "/" + nn);

	}

	public double getPosPrecision() {
		return posPrecision;
	}

	public double getNegPrecision() {
		return negPrecision;
	}

	public double getPosRecall() {
		return posRecall;
	}

	public double getNegRecall() {
		return negRecall;
	}

	public double getPosF1() {
		return posF1;
	}

	public double getNegF1() {
		return negF1;
	}
}
